package com.cgw.auth.advice;

import com.cgw.auth.constance.AuthConst;
import com.cgw.auth.expection.AuthException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.gavin.core.domain.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ResponseExceptionAdvice
 *
 * @author grl
 * @date 2024/1/4
 */
@Slf4j
@ResponseBody
@ControllerAdvice
@RequiredArgsConstructor
public class AuthResponseExceptionAdvice {

    private final HttpServletRequest request;

    @ExceptionHandler(value = AuthException.class)
    public R authExceptionHandler(AuthException e) {
        String appId = request.getHeader(AuthConst.Header_APPID);
        String signature = request.getHeader(AuthConst.Header_SIGNATURE);
        log.error("Auth Exception: {} {} {}  appKey {} signature {}", e.getModule(), e.getMsg(), e.getArgs(),appId,signature, e);
        return R.fail(e.getCode(), e.getMessage());
    }
}
